Confusion Matrix and Statistics
Reference
Prediction Afro House Deep House Melodic House
Afro House 17 2 3
Deep House 3 16 3
Melodic House 1 1 9
Overall Statistics
Accuracy : 0.7636
95% CI : (0.6298, 0.8677)
No Information Rate : 0.3818
P-Value [Acc > NIR] : 9.373e-09
Kappa : 0.6389
Mcnemar's Test P-Value : 0.5319
Statistics by Class:
Class: Afro House Class: Deep House Class: Melodic House
Sensitivity 0.8095 0.8421 0.6000
Specificity 0.8529 0.8333 0.9500
Pos Pred Value 0.7727 0.7273 0.8182
Neg Pred Value 0.8788 0.9091 0.8636
Prevalence 0.3818 0.3455 0.2727
Detection Rate 0.3091 0.2909 0.1636
Detection Prevalence 0.4000 0.4000 0.2000
Balanced Accuracy 0.8312 0.8377 0.7750
Accuracy: 76.36%
This analysis employs a Random Forest algorithm to explore genre classification. We’ve trained our model to differentiate between the nuances of Afro House, Deep House, and Melodic House, drawing insight from a diverse range of audio features. By analyzing elements like tempo, energy, danceability, and more, we seek to pinpoint the factors that determine a song’s genre assignment.
The Random Forest model achieves a solid accuracy of 76.36%. This demonstrates its ability to correctly identify genres more often than not, offering a useful tool for music analysis and classification. Removing features based on the feature selection did not improve the accuracy so therefore I have included all of the features.
Our feature importance plot reveals the most influential variables in the model’s decision-making. Unsurprisingly, tempo stands as a key factor, confirming the importance of BPM in electronic music genres. Interestingly, instrumentalness and speechiness also contribute significantly, highlighting the complex interplay of elements in genre definition.
Overall, the classifier’s performance and these insights help us dissect the characteristic elements of house music subgenres. Such understanding could potentially improve music recommendation systems and offer guidance for music production.
My corpus is a collection with songs of three different genres, namely: Afro House, Deep House, and Melodic House. These genres are all subgenres of house, which makes them interesting to study to find out what distinguishes these genres. Each genre has its own unique blend of rhythm, melodies, and textures which make them very suitable for analysis.
Afro House, with its rhythmic energy and African influences, contrasts with Melodic House’s emotive soundscapes and Deep House’s soulful grooves. This selection aims to explore the auditory and emotional distinctions and intersections among these genres. I expect differences in their rhythmic structure; Afro House has more complex rhythms, whereas Deep House and Melodic House have simpler rhythms. The mood of the genres also differs, from Deep House’s mellow vibes to Afro House’s energy and Melodic House’s ethereal qualities, yet their base structure is similar.
My corpus consists of dozens of songs for every genre, aiming for a diverse range of artists and songs that represent each genre. However a potential gap in my corpus is the focus on relatively recent releases which does not take the history of each genre into account. Also my corpus does potentially miss niche songs that could offer additional insights into each genre.
Typical songs for each genre are: Ben Bohmer - Beyond Beliefs for Melodic House, Rampa - Champion for Afro House, and Mahalo - Home for Deep House, embodying each genre’s core values. Atypical songs like Notre Dame - Yumi - edit for Melodic House, Sebjak - Somebody - edit for Afro House, and Hannah Laing - Good Love for Deep House, are chosen for their unusual rhythm and tempo, offering additional insights into each genre.
This plot is a scatterplot displaying the relationship between ‘Energy’ and ‘Valence’ for the three different genres of music: Afro House, Deep House, and Melodic House. Each panel represents one of the genres, plotted with ‘Energy’ on the x-axis and ‘Valence’ on the y-axis. The data points for Afro House are colored blue, for Deep House they are red, and for Melodic House they are green. The spread of data points in each genre-specific panel indicates the distribution of the tracks according to their energy and valence characteristics within that genre.
It is interesting to see how the three different genres I chose for my analysis compare in terms of their energy and valence. This plot provides insights into the emotional characteristics of each genre. For example, we can visually conclude that Melodic House is considered the least positive of the three genres according to Spotify. This makes sense since it’s the more emotional genre.
This plot is a heatmap that visualizes the intensity of pitch classes over time within the song “Breathing” by Ben Böhmer. The choice for this song was not because it was a pitch related outlier, but because it was a tempo related outlier. The horizontal axis represents time in seconds, and the vertical axis indicates the pitch classes. Intensity levels are depicted through a color gradient, transitioning from blue for lower intensities to yellow for higher intensities.
The analysis of the normalized distribution of musical keys across Afro House, Deep House, and Melodic House genres reveals some intriguing patterns. C# emerges as the predominant key in both Afro House and Melodic House, indicating a preference for its sound qualities in these genres. The choice of key can affect the mood and energy of the music. C#, being a semi-tone higher than C, is often considered to have a bright and lively quality which might resonate well with the energetic and emotive nature of Afro House and the ethereal, uplifting atmosphere of Melodic House.
In contrast, G is the most popular key in Deep House, which may underscore the genre’s tendency towards a deeper, more mellow sonic palette. The key of G is often associated with a warm and inviting tone, which complements Deep House’s soulful and laid-back vibe. This key can provide a comfortable harmonic foundation for the smooth grooves and relaxed rhythms characteristic of this genre.
The preference for these keys suggests that artists and producers are likely to lean towards certain tonalities that align with the expressive needs of their genre. While this trend does not dictate the creative choices made in music production, it highlights the subtle ways in which key selection can contribute to genre-specific atmospheres and listener expectations.
These are the self-similarity matrices for Chroma and Timbre for our tempo outlier ‘Breathing’ by Ben Bohmer.
Afro House average BPM: 121.1
Deep House average BPM:
126.31
Melodic House average BPM: 123.13
In
electronic music, a track’s tempo is its pulse – the driving force that
sets the mood on the dance floor. Within the world of house music, even
subtle changes in beats per minute (BPM) dramatically alter the
atmosphere. Let’s dive into three subgenres to see how:
From the vibrant energy of Afro House to the pulsating power of Deep House and the evocative soundscapes of Melodic House, tempo plays a crucial role in shaping the unique feel of each subgenre.
---
title: "Analysis of Genre Audio Features"
output:
flexdashboard::flex_dashboard:
css: dashboard-styles.css
theme: simplex
social: menu
source: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(spotifyr)
library(dplyr)
library(ggplot2)
library(reshape2)
library(tidyr)
library(ggforce)
library(tibble)
library(purrr)
library(compmus)
library(randomForest)
library(caret)
```
```{r}
Sys.setenv(SPOTIFY_CLIENT_ID = Sys.getenv("SPOTIFY_CLIENT_ID"))
Sys.setenv(SPOTIFY_CLIENT_SECRET = Sys.getenv("SPOTIFY_CLIENT_SECRET"))
access_token <- get_spotify_access_token()
# Fetching data
playlist_id <- "1RhRAqYJA1mwmBtk4mXRju"
username <- "quintijn.kroesbergen"
all_audio_features_df <- get_playlist_audio_features(username, playlist_uris = c(playlist_id), authorization = access_token)
```
```{r}
# Assigning genres
all_audio_features_df$genre <- NA
all_audio_features_df$genre[1:107] <- 'Afro House'
all_audio_features_df$genre[108:206] <- 'Deep House'
all_audio_features_df$genre[207:284] <- 'Melodic House'
muted_colors <- c('Afro House' = '#377eb8', # Muted blue
'Deep House' = '#e41a1c', # Muted red
'Melodic House' = '#4daf4a') # Muted green
```
# Decoding the Dance Floor: Machine Learning Insights into House Subgenres
Column {data-width=750 .plot-column}
-------
```{r}
# Set seed for reproducibility
set.seed(123)
# Define the target and features
target <- "genre"
selected_features <- c("danceability", "energy", "key", "loudness", "mode",
"speechiness", "acousticness", "instrumentalness",
"liveness", "valence", "tempo")
# Splitting the data into training and testing sets
indexes <- createDataPartition(all_audio_features_df[[target]], p = .8, list = FALSE)
train_data <- all_audio_features_df[indexes, ]
test_data <- all_audio_features_df[-indexes, ]
# Select only the desired features plus the target variable for both sets
train_data_selected <- train_data[, c(target, selected_features)]
test_data_selected <- test_data[, c(target, selected_features)]
# Convert genre to a factor
train_data_selected[[target]] <- as.factor(train_data_selected[[target]])
test_data_selected[[target]] <- as.factor(test_data_selected[[target]])
# Train the Random Forest model on the selected features
rf_model <- randomForest(as.formula(paste(target, "~ .")), data=train_data_selected,
importance=TRUE, ntree=500)
varImpPlot(rf_model, type=2, main="Feature Importance")
```
Column {data-width=500 .discussion-column}
-------
```{r}
# Predictions on the test set
predictions <- predict(rf_model, newdata=test_data_selected)
# Evaluation with confusion matrix
confusion <- confusionMatrix(predictions, test_data_selected[[target]])
# Print the confusion matrix
print(confusion)
# Extract overall accuracy from the confusion matrix
accuracy <- confusion$overall['Accuracy']
```
Column {data-width=400 .discussion-column}
-------
```{r}
cat(sprintf("Accuracy: %.2f%%", accuracy * 100))
```
This analysis employs a Random Forest algorithm to explore genre classification. We've trained our model to differentiate between the nuances of Afro House, Deep House, and Melodic House, drawing insight from a diverse range of audio features. By analyzing elements like tempo, energy, danceability, and more, we seek to pinpoint the factors that determine a song's genre assignment.
The Random Forest model achieves a solid accuracy of 76.36%. This demonstrates its ability to correctly identify genres more often than not, offering a useful tool for music analysis and classification. Removing features based on the feature selection did not improve the accuracy so therefore I have included all of the features.
Our feature importance plot reveals the most influential variables in the model's decision-making. Unsurprisingly, tempo stands as a key factor, confirming the importance of BPM in electronic music genres. Interestingly, instrumentalness and speechiness also contribute significantly, highlighting the complex interplay of elements in genre definition.
Overall, the classifier's performance and these insights help us dissect the characteristic elements of house music subgenres. Such understanding could potentially improve music recommendation systems and offer guidance for music production.
# Introduction of Corpus
My corpus is a collection with songs of three different genres, namely: Afro House, Deep House, and Melodic House. These genres are all subgenres of house, which makes them interesting to study to find out what distinguishes these genres. Each genre has its own unique blend of rhythm, melodies, and textures which make them very suitable for analysis.
Afro House, with its rhythmic energy and African influences, contrasts with Melodic House's emotive soundscapes and Deep House's soulful grooves. This selection aims to explore the auditory and emotional distinctions and intersections among these genres. I expect differences in their rhythmic structure; Afro House has more complex rhythms, whereas Deep House and Melodic House have simpler rhythms. The mood of the genres also differs, from Deep House's mellow vibes to Afro House's energy and Melodic House's ethereal qualities, yet their base structure is similar.
My corpus consists of dozens of songs for every genre, aiming for a diverse range of artists and songs that represent each genre. However a potential gap in my corpus is the focus on relatively recent releases which does not take the history of each genre into account. Also my corpus does potentially miss niche songs that could offer additional insights into each genre.
Typical songs for each genre are: Ben Bohmer - Beyond Beliefs for Melodic House, Rampa - Champion for Afro House, and Mahalo - Home for Deep House, embodying each genre's core values. Atypical songs like Notre Dame - Yumi - edit for Melodic House, Sebjak - Somebody - edit for Afro House, and Hannah Laing - Good Love for Deep House, are chosen for their unusual rhythm and tempo, offering additional insights into each genre.
# Harmony in Motion: Exploring Genre Energies and Emotions
## Visualization and Description of first visualisation
Column {data-width=750 .plot-column}
-------
```{r, align='center'}
# Plotting the graph
ggplot(all_audio_features_df, aes(x = energy, y = valence, color = genre)) +
geom_point() +
labs(title = "Energy vs Valence by Genre",
x = "Energy",
y = "Valence",
color = "Genre") +
scale_color_manual(values = muted_colors) + # Use the muted colors here
theme_minimal() +
theme(legend.position = "right")
```
Column {data-width=250 .discussion-column}
-------
This plot is a scatterplot displaying the relationship between 'Energy' and 'Valence' for the three different genres of music: Afro House, Deep House, and Melodic House. Each panel represents one of the genres, plotted with 'Energy' on the x-axis and 'Valence' on the y-axis. The data points for Afro House are colored blue, for Deep House they are red, and for Melodic House they are green. The spread of data points in each genre-specific panel indicates the distribution of the tracks according to their energy and valence characteristics within that genre.
It is interesting to see how the three different genres I chose for my analysis compare in terms of their energy and valence. This plot provides insights into the emotional characteristics of each genre. For example, we can visually conclude that Melodic House is considered the least positive of the three genres according to Spotify. This makes sense since it's the more emotional genre.
# Sonic Spectrum: Mapping Pitch Class Intensity Over Time
## Visualisation
Column {data-width=750 .plot-column}
-------
```{r, align='center'}
track_id <- "1MvLmHeLkaNgUScgbUVnWJ"
audio_analysis <- get_tidy_audio_analysis(track_id)
# Select and unnest segments, then select start, duration, and pitches
chroma_data <-
audio_analysis %>%
select(segments) %>%
unnest(segments) %>%
select(start, duration, pitches)
# Process and plot chroma data
chroma_plot <-
chroma_data %>%
mutate(pitches = map(pitches, compmus_normalise, "euclidean")) %>%
compmus_gather_chroma() %>%
ggplot(
aes(
x = start + duration / 2,
width = duration,
y = pitch_class,
fill = value
)
) +
geom_tile() +
labs(
x = "Time (s)",
y = NULL,
fill = "Magnitude",
title = "Chroma Features Over Time for \nfor 'Breathing' by Ben Bohmer"
) +
theme_minimal() +
scale_fill_viridis_c()
# Print the plot
print(chroma_plot)
```
Column {data-width=250 .discussion-column}
-------
This plot is a heatmap that visualizes the intensity of pitch classes over time within the song "Breathing" by Ben Böhmer. The choice for this song was not because it was a pitch related outlier, but because it was a tempo related outlier. The horizontal axis represents time in seconds, and the vertical axis indicates the pitch classes. Intensity levels are depicted through a color gradient, transitioning from blue for lower intensities to yellow for higher intensities.
# Histograms of keys for the genres
## Visualisation
Column {data-width=500 .plot-column}
-------
```{r}
key_names <- c('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B')
all_audio_features_df$key_name <- key_names[all_audio_features_df$key + 1]
# Calculate the count of tracks in each key by genre
counts <- all_audio_features_df %>%
group_by(genre, key_name) %>%
summarise(count = n(), .groups = 'drop')
# Calculate the total count of tracks in each genre
total_counts <- counts %>%
group_by(genre) %>%
summarise(total = sum(count), .groups = 'drop')
# Join the counts with total counts and calculate the proportion
counts <- counts %>%
left_join(total_counts, by = "genre") %>%
mutate(proportion = count / total)
# Calculate the most popular key for each genre
most_popular_keys <- counts %>%
group_by(genre) %>%
top_n(1, wt = proportion) %>%
ungroup() %>%
select(genre, key_name, proportion)
ymax_limit <- max(counts$proportion) * 1.3
# Create the histogram plot
p <- ggplot(counts, aes(x = key_name, y = proportion, fill = genre)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(title = "Normalized Distribution of Keys by Genre",
x = "Musical Key",
y = "Proportion of Tracks") +
scale_fill_manual(values = muted_colors) +
theme_minimal() +
theme(legend.position = "bottom") +
ylim(0, ymax_limit) # Extend y-axis limit
# Add annotations for the most popular keys
p + geom_text(data = most_popular_keys, aes(x = key_name, y = proportion, label = key_name),
position = position_dodge(width = 0.9), vjust = -0.5, color = "black", fontface = "bold")
```
Column {data-width=500 .discussion-column}
-------
The analysis of the normalized distribution of musical keys across Afro House, Deep House, and Melodic House genres reveals some intriguing patterns. C# emerges as the predominant key in both Afro House and Melodic House, indicating a preference for its sound qualities in these genres. The choice of key can affect the mood and energy of the music. C#, being a semi-tone higher than C, is often considered to have a bright and lively quality which might resonate well with the energetic and emotive nature of Afro House and the ethereal, uplifting atmosphere of Melodic House.
In contrast, G is the most popular key in Deep House, which may underscore the genre's tendency towards a deeper, more mellow sonic palette. The key of G is often associated with a warm and inviting tone, which complements Deep House's soulful and laid-back vibe. This key can provide a comfortable harmonic foundation for the smooth grooves and relaxed rhythms characteristic of this genre.
The preference for these keys suggests that artists and producers are likely to lean towards certain tonalities that align with the expressive needs of their genre. While this trend does not dictate the creative choices made in music production, it highlights the subtle ways in which key selection can contribute to genre-specific atmospheres and listener expectations.
# Self-similarity for Chroma and Timbre
## Visualisation
Column {data-width=750 .plot-column}
-------
```{r}
track_audio_analysis <- get_tidy_audio_analysis(track_id)
# Example adapted for your selected track (assuming 'track_audio_analysis' contains your data)
bzt <- track_audio_analysis |>
compmus_align(bars, segments) |>
select(bars) |>
unnest(bars) |>
mutate(
pitches = map(segments, compmus_summarise, pitches, method = "acentre", norm = "manhattan"),
timbre = map(segments, compmus_summarise, timbre, method = "mean")
)
# Create self-similarity matrices for both chroma and timbre
ssm_data <- bind_rows(
bzt |> compmus_self_similarity(pitches, "aitchison") |> mutate(d = d / max(d), type = "Chroma"),
bzt |> compmus_self_similarity(timbre, "euclidean") |> mutate(d = d / max(d), type = "Timbre")
)
# Plotting
# Assuming ssm_data is already defined and contains the data for plotting
# Assuming ssm_data is already defined and contains the data for plotting
ssm_data |>
ggplot(
aes(
x = xstart + xduration / 2,
y = ystart + yduration / 2,
width = xduration,
height = yduration,
fill = d
)
) +
geom_tile() +
facet_wrap(~type) +
scale_fill_viridis_c(option = "E", guide = "none") +
theme_minimal() +
theme(
strip.text.x = element_text(size = 10),
plot.title.position = "plot",
# Use this if you do not want to show any axis ticks
) +
labs(
title = "Self-Similarity Matrices for Chroma and Timbre\nfor 'Breathing' by Ben Bohmer",
x = "Time (s)", # Label for x-axis
y = "Time (s)" # Label for y-axis
) +
coord_fixed(ratio = 1) # This enforces a 1:1 aspect ratio
```
Column {data-width=250 .discussion-column}
-------
These are the self-similarity matrices for Chroma and Timbre for our tempo outlier 'Breathing' by Ben Bohmer.
# The Heartbeat of House: Tempo as Genre Signature
## Visualisation
Column {data-width=500 .plot-column}
-------
```{r}
mode_tempi <- all_audio_features_df %>%
group_by(genre, tempo) %>%
summarise(count = n(), .groups = 'drop') %>%
# Get the row with the maximum count for each genre
slice_max(order_by = count, n = 1) %>%
ungroup()
# Create the histogram plot
tempo_histogram <- ggplot(all_audio_features_df, aes(x = tempo, fill = genre)) +
geom_histogram(binwidth = 2, alpha = 0.6, position = "identity") + # Adjust binwidth as necessary
labs(title = "Histogram of Tempi by Genre",
x = "Tempo (BPM)",
y = "Count of Tracks") +
scale_fill_manual(values = muted_colors) +
theme_minimal() +
theme(legend.position = "bottom") +
facet_wrap(~ genre, scales = "free_y") # Separate histogram for each genre
# Print the plot
print(tempo_histogram)
```
```{r}
# Calculate the average BPM for each genre
average_bpm <- all_audio_features_df %>%
group_by(genre) %>%
summarize(average_tempo = mean(tempo, na.rm = TRUE)) %>%
ungroup()
# Create the text output for the average BPM in each genre
average_bpm_text <- average_bpm %>%
mutate(text = paste("<b>", genre, "average BPM:", round(average_tempo, 2), "</b><br>")) %>%
pull(text) %>%
paste(collapse = "")
```
Column {data-width=500 .discussion-column}
-------
```{r, echo=FALSE, results='asis'}
# Print the pre-formatted text with HTML bold tags
cat(average_bpm_text)
```
In electronic music, a track's tempo is its pulse – the driving force that sets the mood on the dance floor. Within the world of house music, even subtle changes in beats per minute (BPM) dramatically alter the atmosphere. Let's dive into three subgenres to see how:
- **Afro House**: Infused with traditional African rhythms, Afro House thrums with an average of 121 BPM. This tempo creates a vibrant yet smooth energy, perfect for dance styles that emphasize fluidity and percussive movement.
- **Deep House**: With a slightly faster pulse (126 BPM on average), Deep House caters to the club scene. Soulful melodies and a driving bassline thrive at this elevated tempo, encouraging sustained, energetic movement.
- **Melodic House**: At an average of 123 BPM, Melodic House finds a middle ground between the other two subgenres. The tempo allows space for both reflective moments and upbeat dance sequences. This complements the genre's focus on emotive melodies and atmospheric soundscapes, creating a vibe that's captivating for both listeners and dancers.
From the vibrant energy of Afro House to the pulsating power of Deep House and the evocative soundscapes of Melodic House, tempo plays a crucial role in shaping the unique feel of each subgenre.